package com.zz.cc.business.app;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.Toast;

import com.google.zxing.client.android.encode.QRCodeEncoder;
import com.zz.cc.business.R;
import com.zz.cc.business.data.FileManager;
import com.zz.common.app.BaseActivity;

public class QrCodeActivity extends BaseActivity {
	
	public static final String INDEX_QR_CODE = "index_qr_code";
	
	private String mQrCode;
	
	private ImageView mQrCodeView;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		Intent intent = getIntent();
		getArgumentsFromIntent(intent);
		
		setContentView(R.layout.activity_qrcode);
		initUI();
		fillView();
	}

	private void initUI() {
		mQrCodeView = (ImageView) findViewById(R.id.qrcode_image);
	}
	
	private void fillView() {
		try {
			String filePath = FileManager.getInstance().getQrCodePath();
			Bitmap b = QRCodeEncoder.encode(mQrCode, filePath);
			mQrCodeView.setImageDrawable(new BitmapDrawable(b));
		} catch(Throwable t) {
			Toast.makeText(this, "生成二维码失败！", Toast.LENGTH_SHORT).show();
		}
	}
	
	private void getArgumentsFromIntent(Intent intent) {
		mQrCode = intent.getStringExtra(INDEX_QR_CODE);
	}
}
